package org.jeecg.modules.yw.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.yw.service.ReportInfoService;
import org.jeecg.modules.yw.service.RequestEnterPriseService;
import org.jeecg.modules.yw.utils.DESUtil;
import org.jeecg.modules.yw.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/app/enterpriseInfo")
@Api(tags = "报表管理")
public class ReportInfoController {
    @Autowired
    private ReportInfoService reportInfoService;
    @Resource RequestEnterPriseService requestEnterPriseService;
    @ApiOperation(value = "人员企业统计")
    @GetMapping("/queryPeople")
    public Result queryPeople(){
        PeopleVo vo=reportInfoService.peopleCount();
        List<InfoVo1> list = new ArrayList<>();
        InfoVo1 infoVo = new InfoVo1();
        infoVo.setName("内勤主管");
        infoVo.setValue(vo.getC1());
        list.add(infoVo);
        infoVo = new InfoVo1();
        infoVo.setName("内勤账号");
        infoVo.setValue(vo.getC2());
        list.add(infoVo);
        infoVo = new InfoVo1();
        infoVo.setName("打胶售后");
        infoVo.setValue(vo.getC3());
        list.add(infoVo);infoVo = new InfoVo1();
        infoVo.setName("安装组长");
        infoVo.setValue(vo.getC4());
        list.add(infoVo);
        infoVo = new InfoVo1();
        infoVo.setName("安装组员");
        infoVo.setValue(vo.getC5());
        list.add(infoVo);
        infoVo = new InfoVo1();
        infoVo.setName("门窗企业");
        infoVo.setValue(vo.getC6());
        list.add(infoVo);
        return Result.OK(list);
    }
    @ApiOperation(value = "订单统计")
    @GetMapping("/monthCount")
    public Result monthCount(){
        LocalDate today = LocalDate.now();
        List<MonthVo> list = reportInfoService.monthCount(today.getYear());
        list =setInfoList(list);
        return Result.OK(list);
    }

    private static List<MonthVo> setInfoList(List<MonthVo> list) {
        List<MonthVo> all=new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            MonthVo monthVo = new MonthVo();
            monthVo.setMonth(i+"");
            monthVo.setCou(0);
            all.add(monthVo);
        }
        Map<String, Integer> map = new HashMap<>();
        for (MonthVo monthVo : list) {
            map.put(monthVo.getMonth(), monthVo.getCou());
        }
        for (MonthVo monthVo : all) {
            Integer integer = map.get(monthVo.getMonth());
            if (integer!=null){
                monthVo.setCou(integer);
            }
        }
        return all;
    }

    @ApiOperation(value = "安装班组工人统计")
    @GetMapping("/monthpeople")
    public Result monthpeople(){
        LocalDate today = LocalDate.now();
        List<MonthVo> list = reportInfoService.monthpeople(today.getYear());
        list =setInfoList(list);
        return Result.OK(list);
    }
    @ApiOperation(value = "打胶工人统计")
    @GetMapping("/monthpeople1")
    public Result monthpeople1(){
        LocalDate today = LocalDate.now();
        List<MonthVo> list = reportInfoService.monthpeople1(today.getYear());
        list =setInfoList(list);
        return Result.OK(list);
    }
    @ApiOperation(value = "已付金额统计")
    @GetMapping("/monthpeople2")
    public Result monthpeople2(){
        LocalDate today = LocalDate.now();
        List<MonthVo> list = reportInfoService.monthpeople2(today.getYear());
        list =setInfoList(list);
        return Result.OK(list);
    }
    @ApiOperation(value = "其他统计")
    @GetMapping("/other")
    public Result other(){
        LocalDate today = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 格式化日期
        String formattedDate = today.format(formatter);
        List<InfoVo> list = new ArrayList<>();
        OtherVo otherVo = reportInfoService.other(today.getYear(),formattedDate);
        InfoVo monthVo = new InfoVo();
        monthVo.setName("total");
        monthVo.setValue(otherVo.getTotal().doubleValue());
        list.add(monthVo);
        monthVo = new InfoVo();
        monthVo.setName("monthpeople");
        monthVo.setValue(otherVo.getMonthpeople().doubleValue());
        list.add(monthVo);
        monthVo = new InfoVo();
        monthVo.setName("yearincome");
        monthVo.setValue(otherVo.getYearincome());
        list.add(monthVo);
        monthVo = new InfoVo();
        monthVo.setName("monthincome");
        monthVo.setValue(otherVo.getMonthincome());
        list.add(monthVo);
        monthVo = new InfoVo();
        monthVo.setName("wc");
        monthVo.setValue(otherVo.getWc());
        list.add(monthVo);
        return Result.OK(list);
    }
    
    @ApiOperation(value = "员工查询申请列表")
    @GetMapping("/queryList")
    public org.jeecg.common.api.vo.Result pageReg(HttpServletRequest request,@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(name = "useTime", required=false) String useTime ) {
        String t = request.getHeader("token");
        String id = DESUtil.decryptCBCId(t);
//        QueryWrapper<ReportInfo> queryWrapper = new QueryWrapper<>();
////        queryWrapper.apply("request r LEFT JOIN requestapprove a ON r.id = a.requestid");
////        queryWrapper.select("r.id,r.toolType,r.toolNo,r.useTime,r.createTime,a.approveStatus,a.approveTime,a.approveUserId");
//        queryWrapper.apply(" request r LEFT JOIN requestapprove a ON r.id = a.requestid");
//        queryWrapper.select("r.enterPriseId,r.toolType,r.toolNo,r.useTime,r.picture,a.approve,a.approveman,a.approvetime")
//        queryWrapper.eq("r.enterPriseId", id);
//        if (StringUtils.isNotBlank(useTime)) {
//            queryWrapper.apply("date_format(r.useTime,'%Y-%m-%d') = {0}", useTime);
//        }
//        queryWrapper.orderByDesc("r.createTime");
        Page<ReportInfoQueryListVo> page = new Page<>(pageNo, pageSize);
        IPage<ReportInfoQueryListVo> result = reportInfoService.getBaseMapper().pageReg(page,id,useTime);
        return org.jeecg.common.api.vo.Result.ok(result);
    }
}
